﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class vacancesconf : Form
    {
        private Dictionary<DateTime, TextBox> calendarintervall = new Dictionary<DateTime, TextBox>();
        private Dictionary<int, moisdesigner> moisdelimiter = new Dictionary<int, moisdesigner>();
        private List<TextBox> moistextbox = null;
        private Dictionary<int,TextBox> debmoitextbox = null;
        const int witdh = 20;
        const int height = 20;
        const int rechtmarge=50;
        const int interlinemarge = 3 * 20;


        public vacancesconf()
        {
            InitializeComponent();
            donnees.ReadXml(Properties.datausers.Default.bddpath + Properties.datausers.Default.fichiername);
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataRow[] vacance= findvacance();
            if (vacance != null)
            {
                 List<List<intervalleDateVacancesUser>> uservacancelist = new List<List<intervalleDateVacancesUser>>();
                List<intervalleDateVacancesUser> courentlist=new List<intervalleDateVacancesUser>();
                String Currentid="init";
                 foreach (DataRow row in vacance)
                 {
                     if (!(row["userid"].ToString().Equals(Currentid)))
                     {
                         if(courentlist.Count>0)
                                uservacancelist.Add(courentlist);

                         courentlist=new List<intervalleDateVacancesUser>();
                         Currentid = row["userid"].ToString();
                     }
                        
                     courentlist.Add(new intervalleDateVacancesUser((DateTime)row["datedebut"],(DateTime)(row["datefin"]),Currentid));                
                 }

                 if (courentlist.Count > 0)
                     uservacancelist.Add(courentlist);

                 constructcalendar(uservacancelist.Count);
                 constructuserdata(uservacancelist);
            }
         



        }

       

        private void constructuserdata(List<List<intervalleDateVacancesUser>> uservacancelist)
        {
            int nbuser=uservacancelist.Count();
            DateTime datedebut = dateTimePicker1.Value.Date;
            DateTime dateFin = dateTimePicker2.Value.Date;
            int sautdeligne = height + interlinemarge + uservacancelist.Count * height;
          int i =1;
            foreach (List<intervalleDateVacancesUser> list in uservacancelist)
            {

                foreach (intervalleDateVacancesUser interval in list)
                {

                    DateTime deb = interval.Debut;
                    DateTime fin = interval.Fin;
                    if ((deb.Date).CompareTo(datedebut)<0)                   
                         deb = datedebut;                   

                    if ((fin.Date).CompareTo(dateFin) > 0)                    
                        fin = dateFin;                  

                     TextBox boxdeb= calendarintervall[deb];
                     TextBox boxfin = calendarintervall[fin];

                     if (boxdeb.Location.Y == boxfin.Location.Y)
                     {
                        
                         TextBox temptextbox = texboxbuilder(boxdeb.Location.X, boxdeb.Location.Y + i * height, boxfin.Location.X - boxdeb.Location.X+witdh, height, null);                       
                         temptextbox.BackColor = Color.Red;
                         this.Controls.Add(temptextbox);

                     }
                     else
                     {
                         int nbboucle = (boxfin.Location.Y - boxdeb.Location.Y) / (height + interlinemarge + nbuser * height);
                         int iboucle = 0;
                         int xfin = 0;
                         for (iboucle = 0; iboucle <= nbboucle; iboucle++)
                         {
                             if (iboucle == nbboucle)
                                 xfin = boxfin.Location.X;
                             else
                                 xfin = moisdelimiter[boxdeb.Location.Y].Finx;

                             TextBox temptextbox = texboxbuilder(boxdeb.Location.X, boxdeb.Location.Y + i * height, xfin - boxdeb.Location.X, height, null);                   
                             temptextbox.BackColor = Color.Red;
                             this.Controls.Add(temptextbox);

                             boxdeb = debmoitextbox[boxdeb.Location.Y + (height + interlinemarge + nbuser * height)];
                         }
                     }

                }
                i++;
            }
        }


        private void constructcalendar(int lignnb)
        {
            
            int curantx=label3.Location.X;
            int curanty=label3.Location.Y+label3.Height+30;
            Boolean first = true;
            DateTime datedebut = dateTimePicker1.Value.Date;
            DateTime dateFin = dateTimePicker2.Value.Date;
            List<moisdesigner> mois = new List<moisdesigner>();        
            calendarintervall = new Dictionary<DateTime, TextBox>();
            moisdelimiter = new Dictionary<int, moisdesigner>();
            moistextbox = new List<TextBox>();
            debmoitextbox = new Dictionary<int, TextBox>(); 

            TextBox temptextbox = null;
            DateTime cursordate = datedebut;
            moisdesigner currantmois = new moisdesigner(curantx, curanty); 

             while (cursordate.CompareTo(dateFin) <=0 )
            {
                temptextbox = texboxbuilder(curantx, curanty, witdh, height, cursordate.Day.ToString());
                if (first) {
                    debmoitextbox.Add(curanty, temptextbox);
                    first = false;
                }
                
                this.Controls.Add(temptextbox);
                 
                calendarintervall.Add(cursordate, temptextbox);
                curantx = curantx + witdh;
                if (curantx >= this.Width - rechtmarge)
                {
                    currantmois.fin(curantx, curanty);
                    currantmois.Text = cursordate.Month.ToString();
                    mois.Add(currantmois);
                    moisdelimiter.Add(curanty, currantmois);
                    curanty = curanty + height + interlinemarge + lignnb * height;
                    curantx = label3.Location.X;
                    currantmois = new moisdesigner(curantx, curanty);
                    first = true;
                }
                cursordate = cursordate.AddDays(1);
                if (cursordate.Day == 1)
                {
                    currantmois.fin(curantx, curanty);
                    currantmois.Text = (cursordate.Month-1).ToString();
                    mois.Add(currantmois);                    
                    currantmois = new moisdesigner(curantx, curanty);                   

                }


            }

             currantmois.fin(curantx, curanty);
             currantmois.Text = (cursordate.Month ).ToString();
             mois.Add(currantmois);
             moisdelimiter.Add(curanty, currantmois);
             String text = "";
             TextBox temptextbo = null;
             foreach (moisdesigner moistemp in mois)
             {

                 text = text + ";;" + moistemp.Originex + ";" + moistemp.Originey + ";" + (moistemp.Finx - moistemp.Originex) + ";" + height;
                 temptextbo = texboxbuilder(moistemp.Originex, moistemp.Originey -height, moistemp.Finx - moistemp.Originex, height, moistemp.Text);                
                 temptextbo.TextAlign = HorizontalAlignment.Center;
                 temptextbo.BackColor = MoiDesign.getcouleur(moistemp.Text);
                 moistextbox.Add(temptextbo);
                 this.Controls.Add(temptextbo);
             
            }        


       }




        private TextBox texboxbuilder(int x, int y, int witdh, int height,String text)
        {
            TextBox temptextbox = new TextBox();
            temptextbox.Location = new System.Drawing.Point(x, y);
            temptextbox.Size = new System.Drawing.Size(witdh, height);
            temptextbox.Text = text;
            return temptextbox;
        }


        private DataRow[] findvacance()
        {
            return findvacance(-1, false);
        }

        private DataRow[] findvacance(int userid)
        {
            return findvacance(userid, true);
        }

        private DataRow[] findvacanceAnderUser(int userid)
        {
            return findvacance(userid, false);
        }



        private DataRow[] findvacance(int userid, Boolean foruser)
        {        
            DateTime datedebut = dateTimePicker1.Value;
            DateTime dateFin = dateTimePicker2.Value;
            String userconditiontest="<>";
            if (foruser)
                userconditiontest = "=";

             String usercondition="";
            if(userid>=0)
                usercondition = " and userid " + userconditiontest + userid;

            DataRow[] rows = null;
            rows = donnees.vacance.Select("#" + datedebut.ToString("MM/dd/yyyy") + " # <= datefin and  datedebut <= #" + dateFin.ToString("MM/dd/yyyy") + "# " + usercondition, "userid ASC");   
            return rows;
        }



        private void reset_Click(object sender, EventArgs e)
        {
            ICollection<TextBox> texboxlist = calendarintervall.Values;
            foreach (TextBox textbox in texboxlist) {
                textbox.Dispose();
            }

            if (moistextbox != null)
            {
                foreach (TextBox textbox in moistextbox)
                {
                    textbox.Dispose();
                }
            }
                


            
        }


        private void button2_Click(object sender, EventArgs e)
        {

            donnees.WriteXml(Properties.datausers.Default.bddpath + Properties.datausers.Default.fichiername);
        }


     

    }
}
